Scalable, real-time messaging system

ABSTRACT

Methods, systems, and apparatus, including computer programs encoded on a computer storage medium, for storing messages of each of a plurality of channels in one or more respective buffers, wherein each buffer comprises a respective buffer time-to-live, wherein each buffer comprises a plurality of blocks, wherein each block of the plurality of blocks stores one or more of the messages, wherein each block comprises a respective block time-to-live, and wherein the block time-to-live is different than the buffer time-to-live. The method may also include the action of sending, by the one or more computer processors, the messages for a channel of the plurality of channels retrieved from one or more blocks that have not expired.

CROSS REFERENCE TO RELATED APPLICATION

This application is a continuation of U.S. application Ser. No.14/821,421, filed Aug. 7, 2015, the entire contents of which areincorporated by reference herein.

BACKGROUND

This specification relates to a data communication system and, inparticular, a system that implements real-time, scalablepublish-subscribe messaging system.

The publish-subscribe pattern (or “PubSub”) is a data communicationmessaging arrangement implemented by software systems where so-calledpublishers publish messages to topics and so-called subscribers receivethe messages pertaining to particular topics to which they aresubscribed. There can be one or more publishers per topic and publishersgenerally have no knowledge of which subscribers, if any, will receivethe published messages. Some PubSub systems do not cache messages orhave small caches meaning that subscribers may not receive messages thatwere published before the time of subscription to a particular topic.PubSub systems can be susceptible to performance instability duringsurges of message publications or as the number of subscribers to aparticular topic increases.

SUMMARY

In general, one aspect of the subject matter described in thisspecification can be embodied in methods that include the actions ofstoring messages of each of a plurality of channels in one or morerespective buffers, wherein each buffer comprises a respective buffertime-to-live, wherein each buffer comprises a plurality of blocks,wherein each block of the plurality of blocks stores one or more of themessages, wherein each block comprises a respective block time-to-live,and wherein the block time-to-live is different than the buffertime-to-live. The method may also include the action of sending, by theone or more computer processors, the messages for a channel of theplurality of channels retrieved from one or more blocks that have notexpired. Other embodiments of this aspect include corresponding systems,apparatus, and computer programs.

These and other aspects can optionally be included in other embodiments.The details of one or more embodiments of the subject matter describedin this specification are set forth in the accompanying drawings and thedescription below. Other features, aspects, and advantages of thesubject matter will become apparent from the description, the drawings,and the claims.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1A illustrates an example system that supports the PubSubcommunication pattern.

FIG. 1B illustrates functional layers of software on an example clientdevice.

FIG. 2 is a diagram of an example messaging system.

FIG. 3A is a data flow diagram of an example method for writing data toa streamlet.

FIG. 3B is a data flow diagram of an example method for reading datafrom a streamlet.

FIG. 4A is a data flow diagram of an example method for publishingmessages to a channel of a messaging system.

FIG. 4B is a data flow diagram of an example method for subscribing to achannel of a messaging system.

FIG. 4C is an example data structure for storing messages of a channelof a messaging system.

FIG. 5 is a flowchart of an example method for storing messages in amessaging system.

DETAILED DESCRIPTION

FIG. 1A illustrates an example system 100 that supports the PubSubcommunication pattern. Publisher (e.g., Publishers 1-N) can publishmessages to named channels (e.g., Channels 1-N) by way of the system 100(also referred to as “messaging system” hereafter). A message caninclude any type of information including one or more of the following:text, image content, sound content, multimedia content, video content,binary data, and so on. Other types of message data are possible.Subscribers (e.g., Subscribers 1-N) can subscribe to a named channelusing the system 100 and start receiving messages which occur after thesubscription request or from a given position (e.g., a message number ortime offset). A client can be both a publisher and a subscriber.

Depending on the configuration, a PubSub system can be categorized asfollows:

-   -   One to One (1:1). In this configuration there is one publisher        and one subscriber per channel. An example use case is private        messaging.    -   One to Many (1:N). In this configuration there is one publisher        and multiple subscribers per channel. Example use cases are        broadcasting messages e.g., stock prices)    -   Many to Many (M:N). In this configuration there are many        publishers publishing to a single channel. The messages are then        delivered to multiple subscribers. Example use cases are map        applications.

There is no separate operation needed to create a named channel. Achannel is created implicitly when the channel is subscribed to or whena message is published to the channel. In some implementations, channelnames can be qualified by a name space. A name space includes one ormore channel names. Different name spaces can have the same channelnames without causing ambiguity. The name space name can be a prefix ofa channel name where the name space and channel name are separated by adot. In some implementations, name spaces can be used when specifyingchannel authorization settings. For instance, the messaging system 100may have appl.foo and appl.system.notifications channels where “appl” isthe name of the name space. The system can allow clients to subscribeand publish to the appl.foo channel. However, clients can subscribe, butnot publish, to the appl.system.notifications channel.

FIG. 1B illustrates functional layers of software on an example clientdevice. A client device (e.g., client 102) is a data processingapparatus such as, for example, a personal computer, a laptop computer,a tablet computer, a smart phone, a smart watch, or a server computer.Other types of client devices are possible. The application layer 104includes the end-user application(s) that will integrate with the system100. The messaging layer 106 is a programmatic interface for theapplication layer 104 to utilize services of the system 100 such aschannel subscription, message publication, message retrieval, userauthentication, and user authorization. In some implementations, themessages passed to and from the messaging layer 106 are encoded asJavaScript Object Notation (JSON) objects. Other message encodingschemes are possible.

The operating system 108 includes the operating system software on theclient 102. In various implementations, messages can be sent andreceived to/from the system 100 using persistent or non-persistentconnections. Persistent connections can be created using, for example,network sockets. A transport protocol such as TCP/IP layer 112implements the Transport Control Protocol/Internet Protocolcommunication with the system 100 that can be used by the messaginglayer 106 to send messages over connections to the system 100. Othercommunication protocols are possible including, for example, UserDatagram Protocol (UDP). In further implementations, an optionalTransport Layer Security (TLS) layer 110 can be employed to ensure theconfidentiality of the messages.

FIG. 2 is a diagram of an example messaging system 100. The messagingsystem 100 provides functionality for implementing PubSub communicationpatterns. The messaging system 100 includes software components andstorage that can be deployed at one or more data centers 122 in one ormore geographic locations, for example. The messaging system 100includes multiplexer (MX) nodes 202, 204 and 206, queue (Q) nodes 208,210 and 212, one or more channel manager nodes (e.g., channel managers214, 215), and optionally one or more cache (C) nodes 220 and 222. Eachnode can execute in a virtual machine or on a physical machine (e.g., adata processing apparatus). Each MX node serves as a termination pointfor one or more publisher and/or subscriber connections through theexternal network 216. The internal communication among MX nodes, Qnodes, C nodes, and the channel manager, is conducted over an internalnetwork 218, for example. By way of illustration, MX node 204 can be theterminus of a subscriber connection from client 102. Each Q node bufferschannel data for consumption by the MX nodes. An ordered sequence ofmessages published to a channel is a logical channel stream. Forexample, if three clients publish messages to a given channel, thecombined messages published by the clients comprise a channel stream.Messages can be ordered in a channel stream, for example, by time ofpublication by the client, by time of receipt by an MX node, or by timeof receipt by a Q node. Other ways for ordering messages in a channelstream are possible. In the case where more than one message would beassigned to the same position in the order one of the messages can bechosen (e.g., randomly) to have a later sequence in the order. Eachchannel manager node is responsible for managing Q node load bysplitting channel streams into so-called streamlets. Streamlets arediscussed further below. The optional C nodes provide caching and loadremoval from the Q nodes.

In the example messaging system 100, one or more client devices(publishers and/or subscribers,) establish respective persistentconnections (e.g., TCP connections) to an MX node (e.g., MX 202, 204and/or 206). The MX node serves as a termination point for theseconnections. For instance, external messages (e.g., between respectiveclient devices and the MX node) carried by these connections can beencoded based on an external protocol (e.g., SSON). The MX nodeterminates the external protocol and translates the external messages tointernal communication, and vice versa. The MX nodes 202, 204 and 206publish and subscribe to streamlets on behalf of clients. In this way,an MX node can multiplex and merge requests of client devicessubscribing for or publishing to the same channel, thus representingmultiple client devices as one, instead of one by one.

In the example messaging system 100, a Q node (e.g., Q nodes 208, 210and/or 212)) can store one or more streamlets of one or more channelstreams. A streamlet is a data buffer fur a portion of a channel stream.A streamlet will close to writing when its storage is full. A streamletwill close to reading and writing and be de-allocated when itstime-to-live (TTL) has expired. For example, a streamlet can have amaximum size of 1 MB and a TTL of three minutes. Different channels canhave streamlets limited by different TTLs. For instance, streamlets inone channel can exist for up to three minutes, while streamlets anotherchannel can exist for up to 10 minutes. In various implementations, astreamlet corresponds to a computing process running on a Q node. Thecomputing process can be terminated after the streamlet's TTL hasexpired, thus freeing up computing resources (for the streamlet) back tothe Q node, for example.

When receiving a publish request from client 102, an MX node (e.g., MX204) makes a request to a channel manager (e.g., channel manager 214) togrant access to a streamlet to write the message being published.However, if the MX node has already been granted write access to astreamlet for the channel (and the channel has not been closed towriting) the MX node can write the message to that streamlet withouthaving to request a grant to access the streamlet. Once a message iswritten to a streamlet for a channel the message can be read by MX nodesand provided to subscribers of that channel.

Similarly, when receiving a channel subscription request from a clientdevice, an MX node makes a request to a channel manager to grant accessto a streamlet for the channel from which messages are read. If the MXnode has already been granted read access to a streamlet for the channel(and the channel's TTL has not been closed to reading) the MX node canread messages from the streamlet without having to request a grant toaccess the streamlet. The read messages can then be forwarded to clientdevices that have subscribed to the channel. In various implementations,messages read from streamlets are cached by MX nodes so that MX nodescan reduce the number of times messages are read from the streamlets.

In implementations, an MX node can request a grant from the channelmanager that allows the MX node to store a block of data into astreamlet on a particular Q node that stores streamlets of a particularchannel. Example streamlet grant request and grant data structures areas follows:

StreamletGrantRequest = { ″channel″: string( ) ″mode″: ″read″ | ″write″“position”: 0 } StreamletGrantResponse = { ″streamlet-id″:″abcdef82734987″, ″limit-size″: 2000000, # 2 megabytes max ″limit-msgs″:5000, # 5 thousand messages max ″limit-life″: 4000, # the grant is validfor 4 seconds “q-node″: string( ) “position”: 0 }

The StreamletGrantRequest data structure stores the name of the streamchannel and a mode indicating whether the MX node intends on readingfrom or writing to the streamlet. The MX node sends theStreamletGrantRequest to a channel manager node. The channel managernode, in response, sends the MX node a StreamletGrantResponse datastructure. The StreamletGrantResponse contains an identifier of thestreamlet (streamlet-id), the maximum size of the streamlet(limit-size), the maximum number of messages that the streamlet canstore (limit-msgs), the TTL (limit-life), and an identifier of a Q node(q-node) on which the streamlet resides. The StreamletGrantRequest andStreamletGrantResponse can also have a position field that points to aposition in a streamlet (or a position in a channel) for reading fromthe streamlet.

A grant becomes invalid once the streamlet has closed. For example, astreamlet is closed to reading and writing once the streamlet's TTL hasexpired or when the streamlet's storage is full. When a grant becomesinvalid, the MX node can request a new grant from the channel manager toread from or write to a streamlet. The new grant will reference adifferent streamlet and will refer to the same or a different Q nodedepending on where the new streamlet resides.

FIG. 3A is a data low diagram of an example method 300 for writing datato a streamlet in various embodiments. In FIG. 3A, when an MX node's(e.g., MX 202) request to write to a streamlet is granted by a channelmanager (e.g., channel manager 214), the MX node 202 establishes aTransmission Control Protocol (TCP) connection with the Q node (e.g., Qnode 208) identified in the grant response received from the channelmanager (302). A streamlet can be written concurrently by multiple writegrants (e.g., for messages published by multiple publishers). Othertypes of connection protocols between the MX node 202 and the Q node 208are possible.

The MX node 202 sends (304) a prepare-publish message with an identifierof a streamlet that the MX node 202 wants to write to the Q node 208.The streamlet identifier and Q node identifier can be provided by thechannel manager in the write grant as described earlier. The Q node 202provides the message to a handler 301 (e.g., a computing process runningon the Q node) for the identified streamlet (306). The handler 301 cansend an acknowledgement to the MX node 202 (308). After receiving theacknowledgement, the MX node 202 starts writing (publishing) messages(e.g., 310, 312, 314, and 318) to the handler 301, which stores thereceived data in the identified streamlet. The handler 301 can also sendacknowledgements (316, 320) to the MX node 202 for the received data. Insome implementations, acknowledgements can be piggy-backed orcumulative. For example, the handler 301 can send an acknowledgement tothe MX node 202 for every predetermined amount of data received (e.g.,for every 100 messages received) or for every predetermined time period(e.g., for every one millisecond). Other acknowledgement schedulingalgorithms, such as Nagle's algorithm, can be used.

If the streamlet can no longer accept published data (e.g., thestreamlet is full), the handler 301 sends a Negative-Acknowledgement(NAK) message (330) indicating a problem, following by an EOF(end-of-file) message (332). In this way, the handler 301 closes theassociation with the MX node 202 for the publish grant. The MX node 202can then request a write grant for another streamlet from a channelmanager if the MX node 202 has additional messages to store.

FIG. 3B is a data flow diagram of an example method 350 for reading datafrom a streamlet in various embodiments. In FIG. 3B, an MX node (e.g.,MX 204) sends a request to a channel manager (e.g., channel manager 214)for reading a particular channel starting from a particular message ortime offset in the channel. The channel manager returns a read grant tothe MX node 204 including an identifier of a streamlet containing theparticular message, a position in the streamlet corresponding to theparticular message, and an identifier of a node (e.g., node 208)containing the particular streamlet. The MX node 204 then establishes aTCP connection with the Q node (352). Other types of connectionprotocols between the MX node 204 and the Q node 208 are possible.

The MX node 204 then sends a subscribe message (354) to the Q node 208with the identifier of the streamlet in the Q node 208 and the positionin the streamlet from which the MX node 204 wants to read (356). The Qnode 208 provides the subscribe message to a handler process 351 for thestreamlet (356). The handler 351 can send an acknowledgement to the MXnode 204 (358). The handler 351 sends messages (360, 364, 366), startingat the position in the streamlet, to the MX node 204. In someimplementations, the handler 351 can send all of the messages in thestreamlet to the MX node 204. After sending the last message in aparticular streamlet, the handler 351 can send a notification of thelast message to the MX node 204. The MX node 204 can send to the channelmanager another request for another streamlet containing a next messagein the particular channel.

If the particular streamlet is closed (e.g., after its TTL has expired),the handler 351 can send an unsubscribe message (390), followed by anEOF message (392), to close the association with the MX node for theread grant. The MIX node 204 can close the association with the handler351 when the MX node moves to another streamlet for messages in theparticular channel (e.g., as instructed, by the channel manager). The MXnode 204 can also close the association with the handler 351 if the MXnode 204 receives an unsubscribe message from a corresponding clientdevice.

In various implementations, a streamlet can be written into and readfrom at the same time instance. For example, there can be a valid readgrant and a valid write grant at the same time instance. In variousimplementations, a streamlet can be read concurrently by multiple readgrants (e.g., for channels subscribed to by multiple publisher clients).The handler of the streamlet can order messages from concurrent writegrants based on, for example, time-of-arrival, and store the messagesbased on the order. In this way, messages published to a channel frommultiple publisher clients can be serialized and stored in a streamletof the channel.

In the messaging, system 100, one or more C nodes (e.g., C node 220) canoffload data transfers from one or more Q nodes. For example, if thereare multiple MX nodes requesting streamlets from Q nodes for aparticular channel, the streamlets can be offloaded and cached in one ormore C nodes. The MX nodes (e.g., as instructed by read grants from achannel manager) can read the streamlets from the C nodes instead.

As described above, messages for a channel in the messaging system 100are ordered in a channel stream. A channel manager (e.g., channelmanager 214) splits the channel stream into fixed-sized streamlets thateach reside on a respective Q node. In this way, storing a channelstream can be shared among many Q nodes; each Q node stores a portion(one or more streamlets) of the channel stream. More particularly, astreamlet can be stored, for example, in registers and/or dynamic memoryelements associated with a computing process on a Q node, thus avoidingthe need to access persistent, slower storage devices such as harddisks. This results in faster message access. The channel manager canalso balance loads among Q nodes in the messaging system 100 bymonitoring respective workloads of the Q nodes and allocating streamletsin a way that avoids overloading any one Q node.

In various implementations, a channel manager maintains a listidentifying each active streamlet, the respective Q node on which thestreamlet resides, and identification of the position of the firstmessage in the streamlet, and whether the streamlet is closed forwriting. In some implementations, Q nodes notify the channel manager andany MX nodes that are publishing to a streamlet that the streamlet isclosed due to being full or when the streamlet's TTL has expired. When astreamlet is closed the streamlet remains on the channel manager's listof active streamlets until the streamlet's TTL has expired so that MXnodes can continue to retrieve messages from the streamlet.

When an MX node requests a write grant for a given channel and there isnot a streamlet for the channel that can be written to, the channelmanager allocates a new streamlet on one of the Q nodes and returns theidentity of the streamlet and the Q node in the StreamletGrantResponseto the MX node. Otherwise the channel manager returns the identity ofthe currently open for writing streamlet and corresponding Q node in theStreamletGrantResponse to the MX node. MX nodes can publish messages tothe streamlet until the streamlet is full or the streamlet's TTL hasexpired, after which a new streamlet can be allocated by the channelmanager.

When an MX node requests a read grant for a given channel and there isnot a streamlet for the channel that can be read from, the channelmanager allocates a new streamlet on one of the Q nodes and returns theidentity of the streamlet and the Q node in the StreamletGrantResponseto the MX node. Otherwise, the channel manager returns the identity ofthe streamlet and Q node that contains the position from which the MXnode wishes to read to the MX node. The Q node can then begin sendingmessages to the MX node from the streamlet beginning at the specifiedposition until there are no more messages in the streamlet to send. Whena new message is published to a streamlet, MX nodes that have subscribedto that streamlet will receive the new message. If a streamlet's TTL hasexpired the handler 351 sends an EOF message (392) to any MX nodes thatare subscribed to the streamlet.

As described earlier in reference to FIG. 2, the messaging system 100can include multiple channel managers (e.g., channel managers 214, 215).Multiple channel managers provide resiliency and prevent single point offailure. For instance, one channel manager can replicate lists ofstreamlets and current grants it maintains to another “slave” channelmanager. As for another example, multiple channel managers cancoordinate operations between them using distributed consensus protocolssuch as Paxos or Raft protocols.

FIG. 4A is a data flow diagram of an example method 400 for publishingmessages to a channel of a messaging s stem. In FIG. 4A, publishers(e.g., publishers 402, 404, 406) publish messages to the messagingsystem 100 described earlier in reference to FIG. 2. For instance,publishers 402 respectively establish connections 411 and send publishrequests to the MX node 202. Publishers 404 respectively establishconnections 413 and send publish requests to the MX node 204. Publishers406 respectively establish connections 415 and send publish requests tothe MX 204. Here, the MX nodes can communicate (417) with a channelmanager (e.g., channel manager 214) and one or more Q nodes (e.g., Qnodes 212 and 208) in the messaging system 100 via the internal network218.

By way of illustration, each publish request (e.g., in JSON key/valuepairs) from a publisher to an MX node includes a channel name and amessage. The MX node (e.g., MX node 202) can assign the message in thepublish request to a distinct channel in the messaging system 100 basedon the channel name (e.g., “foo”) of the publish request. The MX nodecan confirm the assigned channel with the channel manager 214. If thechannel (specified in the subscribe request) does not yet exist in themessaging system 100, the channel manager can create and maintain a newchannel in the messaging system 100. For instance, the channel managercan maintain a new channel by maintaining a list identifying each activestreamlet of the channel's stream, the respective node on which thestreamlet resides, and identification of the positions of the first andlast messages in the streamlet as described earlier.

For messages of a particular channel, the MX node can store the messagesin one or more buffers or streamlets in the messaging system 100. Forinstance, the MX node 202 receives from the publishers 402 requests topublish messages M11, M12, M13, and M14 to a channel foo. The MX node206 receives from the publishers 404 requests to publish messages M78and M79 to the channel foo. The MX node 204 receives from the publishers406 requests to publish messages M26, M27, M28, M29, M30, and M31 to thechannel foo.

The MX nodes can identify one or more streamlets or storing messages forthe channel foo. As described earlier, each MX node can request a writegrant from the channel manager 214 that allows the MX node to store themessages in a streamlet of the channel foo. For instance, the MX node202 receives a grant from the channel manager 214 to write messages M11,M12, M13, and M14 to a streamlet 4101 on the Q node 212. The MX node 206receives a grant from the channel manager 214 to write messages M78 andM79 to the streamlet 4101. Here, the streamlet 4101 is the laststreamlet of a sequence of streamlets of the channel stream 430 storingmessages of the channel foo. The streamlet 4101 has messages (421) ofthe channel foo that were previously stored in the streamlet 4101, butis still open (e.g., the streamlet 4101 still has space for storing moremessages and the streamlet's TTL has not expired).

The MX node 202 can arrange the messages for the channel foo based onthe respective time that each of the messages 422 was received by the MXnode 202, e.g., M11, M13, M14, M12, and store the received messages asarranged in the streamlet 4101. That is, the MX node 202 receives M11first, followed by M13, M14, and M12. Similarly, the MX node 206 canarrange the messages for the channel foo based on their respective timethat each of the messages 423 was received by the MX node 206 (e.g.,M78, M79), and store the received messages 423 as arranged in thestreamlet 4101.

The MX node 202 (or MX node 206) can store the received messages usingthe method for writing data to a streamlet described earlier inreference to FIG. 3A, for example. In various implementations, the MXnode 202 (or MX node 206) can buffer (e.g., in a local data buffer) thereceived messages for the channel foo and store the received messages ina streamlet for the channel foo (e.g., streamlet 4101) when the bufferedmessages reach a predetermined size (e.g., 100 messages) or when apredetermined time (e.g., 50 milliseconds) has elapsed. For instance,the MX node 202 can store in the streamlet 100 messages at a time or in50 milliseconds increments. Other acknowledgement scheduling algorithms,such as Nagle's algorithm, can be used.

In various implementations, the Q node 212 (e.g., a handler) stores themessages of the channel for) in the streamlet 4101 in the order asarranged by the MX node 202 and MX node 206. The Q node 212 stores themessages of the channel foo in the streamlet 4101 in the order the Qnode 212 receives the messages. For instance, assume that the Q node 212receives messages M78 (from the MX node 206) first, followed by messagesM11 and M13 (from the MX node 202), M79 (from the MX node 206), and M14,and M12 (from the MX node 202). The Q node 212 stores in the streamlet4101 the messages in the order as received (e.g., M78, M11, M13, M79,M14, and M12) immediately after the messages 421 that are already storedin the streamlet 4101. In this way, messages published to the channelfoo from multiple publishers (e.g., MX nodes 402, 404) can be serializedin a particular order and stored in the streamlet 4101 of the channelfoo. Different subscribers that subscribe to the channel foo willreceive messages of the channel foo in the same particular order, aswill be described in more detail in reference to FIG. 4B.

In the example of FIG. 4A, at a time instance after the message M12 wasstored in the streamlet 4101, the MX node 204 requests a grant from thechannel manager 214 to write to the channel foo. The channel manager 214provides the MX node 204 a grant to write messages to the streamlet4101, as the streamlet 4101 is still open for writing. The MX node 204arranges the messages for the channel foo based on the respective timethat each message 424 was received by the MX node 204 (M26, M27, M31,M29, M30, M28), and stores the messages as arranged for the channel foo.

By way of illustration, assume that the message M26 is stored to thelast available position of the streamlet 4101. As the streamlet 4101 isnow full, the Q node 212 sends to the MX node 204 a NAK message,following by an EOF message, to close the association with the MX node204 for the write grant, as described earlier in reference to FIG. 3A.The MX node 204 then requests another write grant from the channelmanager 214 for additional messages (e.g., M27, M31, and so on) for thechannel foo.

The channel manager 214 can monitor available Q nodes in the messagingsystem 100 for the Q nodes respective workloads (e.g., how manystreamlets are residing in each Q node). The channel manager 214 canallocate a streamlet for the write request from the MX node 204 suchthat overloading e.g., too many streamlets or too many read or writegrants) can be avoided for any given. Q node. For example, the channelmanager 214 can identify a least loaded Q node in the messaging system100 and allocate a new streamlet on the least loaded Q node for writerequests from the MX node 204. In the example of FIG. 4A, the channelmanager 214 allocates a new streamlet 4102 on the Q node 208 andprovides a write grant to the MX node 204 to write messages for thechannel too to the streamlet 4102. As shown in FIG. 4A, the Q node 208stores in the streamlet 4102 the messages from the MX node 204 in anorder as arranged by the MX node 204: M27, M31, M29, M30, and M28(assuming that there is no other concurrent write grants for thestreamlet 4102 at the moment).

When the channel manager 214 allocates a new streamlet (e.g., streamlet4102) for a request for a grant from an MX node (e.g., MX node 204) towrite to a channel (e.g., foo), the channel manager 214 assigns to thestreamlet its TTL, which will expire after TTLs of other streamlets thatare already in the channel's stream. For instance, the channel manager214 can assign to each streamlet of the channel foo's channel stream aTTL of 3 minutes when allocating the streamlet. That is, each streamletwill expire 3 minutes after it is allocated (created) by the channelmanager 214. Since a new streamlet is allocated after a previousstreamlet is closed (e.g., filled entirely or expired), in this way, thechannel foo's channel stream comprises streamlets that each expiressequentially after its previous streamlet expires. For instance, asshown in an example channel stream 430 of the channel foo in FIG. 4A,streamlet 4098 and streamlets before 4098 have expired (as indicated bythe dotted-lined gray-out boxes). Messages stored in these expiredstreamlets are not available for reading for subscribers of the channelfoo. Streamlets 4099, 4100, 4101, and 4102 are still active (notexpired). The streamlets 4099, 4100, and 4101 are closed for writing,but still are available for reading. The streamlet 4102 is available forreading and writing, at the moment when the message M28 was stored inthe streamlet 4102. At a later time, the streamlet 4099 will expire,following by the streamlets 4100, 4101, and so on.

FIG. 4B is a data flow diagram of an example method for subscribing to achannel of a messaging system. In FIG. 4B, a subscriber 480 establishesa connection 462 with an MX node 461 of the messaging system 100.Subscriber 482 establishes a connection 463 with the MX node 461.Subscriber 485 establishes a connection 467 with an MX node 468 of themessaging system 100. Here, the MX nodes 461 and 468 can respectivelycommunicate (464) with the channel manager 21.4 and one or more Q nodesin the messaging system 100 via the internal network 218.

A subscriber (e.g., subscriber 480) can subscribe to the channel foo ofthe messaging system 100 by establishing a connection (e.g., 462) andsending a request for subscribing to messages of the channel foo to anMX node (e.g., MX node 461). The request (e.g., in JSON key/value pairs)can include a channel name, such as, for example, “foo.” When receivingthe subscribe request, the MX node 461 can send to the channel manager214 a request for a read grant for a streamlet in the channel foo'schannel stream.

By way of illustration, assume that at the current moment the channelfoo's channel stream 431 includes active streamlets 4102, 4103, and4104, as shown in FIG. 4B. The streamlets 4102 and 4103 each are full.The streamlet 4104 stores messages of the channel foo, including thelast message (at the current moment) stored at a position 47731.Streamlets 4101 and streamlets before 4101 are invalid, as theirrespective TTLs have expired. Note that the messages M78, M11, M13, M79,M14, M12, and M26 stored in the streamlet 4101, described earlier inreference to FIG. 4A, are no longer available for subscribers of thechannel foo, since the streamlet 4101 is no longer valid, as its TTL hasexpired. As described earlier, each streamlet in the channel foo'schannel stream has a TTL of 3 minutes, thus only messages (as stored instreamlets of the channel foo) that are published to the channel foo(i.e., stored into the channel's streamlets) no earlier than 3 minutesfrom the current time can be available for subscribers of the channelfoo.

The MX node 461 can request a read grant for all available messages inthe channel foo, for example, when the subscriber 480 is a newsubscriber to the channel foo. Based on the request, the channel manager214 provides the MX node 461 a read grant to the streamlet 4102 (on theQ node 208) that is the earliest streamlet in the active streamlets ofthe channel foo (e.g., the first in the sequence of the activestreamlets). The MX node 461 can retrieve messages in the streamlet 4102from the Q node 208, using the method for reading data from a streamletdescribed earlier in reference to FIG. 3B, for example. Note that themessages retrieved from the streamlet 4102 maintain the same order asstored in the streamlet 4102. In various implementations, when providingmessages stored in the streamlet 4102 to the MX node 461, the Q node 208can buffer (e.g., in a local data buffer) the messages and sends themessages to the MX node 461 when the buffer messages reaches apredetermined size (e.g., 200 messages), or a predetermined time (e.g.,50 milliseconds) has elapsed. For instance, the Q node 208 can send thechannel foo's messages (from the streamlet 4102) to the M X node 461 200messages at a time or in 50 milliseconds increments. Otheracknowledgement scheduling algorithms, such as Nagle's algorithm, can beused.

After receiving the last message in the streamlet 4102, the MX node 461can send an acknowledgement to the Q node 208, and send to the channelmanager 214 another request (e.g., for a read grant) for the nextstreamlet in the channel stream of the channel foo. Based on therequest, the channel manager 214 provides the MX node 461 a read grantto the streamlet 4103 (on Q node 472) that logically follows thestreamlet 4102 in the sequence of active streamlets of the channel foo.The MX node 461 can retrieve messages stored in the streamlet 4103,e.g., using the method for reading data from a streamlet describedearlier in reference to FIG. 3B, until MX node 601 retrieves the lastmessage stored in the streamlet 4103. The MX node 461 can send to thechannel manager 214 yet another request for a read grant or messages inthe next streamlet 4104 (on Q node 474). After receiving the read grant,the MX node 461 retrieves message of the channel foo stored in thestreamlet 4104, until the last message at the position 47731 isretrieved by MX node 461. Similarly, the MX node 468 can retrievemessages from the streamlets 4102, 4103, and 4104 (as shown with dottedarrows in FIG. 4B), and provide the messages to the subscriber 485.

The MX node 461 can send the retrieved messages of the channel foo tothe subscriber 480 (via the connection 462) while receiving the messagesfrom the Q node 208, 472, or 474. In various implementations, the MXnode 461 can store the retrieved messages in a local buffer. In thisway, the retrieved messages can be provided to another subscriber (e.g.,subscriber 482) when the other subscriber subscribes to the channel fooand requests the channel's messages. The MX node 461 can remove messagesstored in the local buffer that each has a time of publication that hasexceeded a predetermined time period. For instance, the MX node 461 canremove messages (stored in the local buffer) with respective times ofpublication exceeding 3 minutes. In some implementations, thepredetermined time period for keeping messages in the local buffer on MXnode 461 can be the same as or similar to the time-to-live duration of astreamlet in the channel foo's channel stream, since at a given moment,messages retrieved from the channel's stream do not include the messagesin streamlets having respective time-to-lives that have already expired.

The messages retrieved from the channel stream 431 and sent to thesubscriber 480 (by the MX node 461) are arranged in the same order asthe messages were stored in the channel stream. For example, messagespublished to the channel foo are serialized and stored in the streamlet4102 in a particular order (e.g., M27, M31, M29, M30, and so on), thenstored subsequently in the streamlet 4103 and the streamlet 4104. The MXnode 461 retrieves messages from the channel stream 431 and provides theretrieved messages to the subscriber 480 in the same order as themessages are stored in the channel stream: M27, M31, M29, M30, and soon, followed by ordered messages in the streamlet 4103, and followed byordered messages in the streamlet 4104.

Instead of retrieving all available messages in the channel stream 431,the MX node 461 can request a read grant for messages stored in thechannel stream 431 starting from a message at particular position (e.g.,position 47202). For example, the position 47202 can correspond to anearlier time instance (e.g., 10 seconds before the current time) whenthe subscriber 480 was last subscribing to the channel foo (e.g., via aconnection to the M X node 461 or another MX node of the messagingsystem 100). The MX node 461 can send a request to the channel manager214 for a read grant for messages starting at the position 47202. Basedon the request, the channel manager 214 provides the MX node 461 a readgrant to the streamlet 4104 (on the Q node 474) and a position on thestreamlet 4104 that corresponds to the channel stream position 47202.The MX node 461 can retrieve messages in the streamlet 4104 startingfrom the provided position, and send the retrieved messages to thesubscriber 480.

As described above in reference to FIGS. 4A and 4B, messages publishedto the channel foo are serialized and stored in the channel's streamletsin a particular order. The channel manager 214 maintains the orderedsequence of streamlets as they are created throughout their respectivetime-to-lives. Messages retrieved from the streamlets by an MX node(e.g., MX node 461 and/or MX node 468) and provided to a subscriber canbe, in some implementations, in the same order as the messages arestored in the ordered sequence of streamlets. In this way, messages sentto different subscribers (e.g., subscriber 480, subscriber 482, orsubscriber 485) can be in the same order as the messages are stored inthe streamlets, regardless which MX nodes the subscribers are connectedto.

In various implementations, a streamlet stores messages in a set ofblocks of messages. Each block stores a number of messages. Forinstance, a block can store two hundred kilobytes of messages. Eachblock has its own time-to-live, which can be shorter than thetime-to-live of the streamlet holding the block. Once a block's TTL, hasexpired, the block can be discarded from the streamlet holding theblock, as described in more detail below in reference to FIG. 4C.

FIG. 4C is an example data structure 490 for storing messages of achannel of a messaging system. As described with the channel foo inreference to FIGS. 4A and 4B, assume that at the current moment thechannel foo's channel stream 432 includes active streamlets 4104 and4105. Streamlet 4103 and streamlets before streamlet 4103 (e.g.,streamlets 4101 and 4102) are invalid, as their respective TTLs haveexpired. The streamlet 4104 has reached maximum capacity (e.g., asdetermined by a corresponding write grant) and is closed for additionalmessage writes. The streamlet 4104 is available for message reads. Thestreamlet 4105 is open and is available for message writes and reads.

By way of illustration, the streamlet 4104 (e.g., a computing processrunning on the Q node 474 shown in FIG. 4B) currently holds two blocksof messages. Block 494 holds messages from channel positions 47301 to47850. Block 495 holds messages from channel positions 47851 to 48000.The streamlet 4105 (e.g., a computing process running on another Q nodein the messaging system 100) currently holds two blocks of messages.Block 496 holds messages from channel positions 48001 to 48200. Block497 holds messages starting from channel position 48201, and stillaccepts additional messages of the channel foo.

When the streamlet 4104 was created (e.g., by a write grant), a block492 was created to store messages from channel positions 47010 to 47100.Later on, after the block 492 had reached its capacity, another block493 was created to store messages (e.g., from channel positions 47111 to47300). Blocks 494 and 495 were subsequently created to store additionalmessages. Afterwards, the streamlet 4104 was closed for additionalmessage writes, and the streamlet 4105 was created with additionalblocks for storing additional messages of the channel foo.

In this example, the respective TTL's of blocks 492 and 493 haveexpired. The messages stored in these two blocks (from channel positions47010 to 47300) are no longer available for reading by subscribers ofthe channel foo. The streamlet 4104 can discard these two expiredblocks. For example, the streamlet 4104 can deallocate the memory spacefor the blocks 492 and 493. The blocks 494 or 495 could become expiredand be discarded by the streamlet 4104, before the streamlet 4104 itselfbecome invalid. Alternatively, streamlet 4104 itself could becomeinvalid before the blocks 494 or 495 become expired. For example, astreamlet can hold one or more blocks of messages, or contain no blockof messages, depending on respective TTLs of the streamlet and blocks.

A streamlet, or a computing process running on a node in the messagingsystem 100, can create a block for storing messages of a channel byallocating a certain size of memory space from the Q node. The streamletcan receive, from an MX node in the messaging system 100, one message ata time and store the received message in the block. Alternatively, theMX node can assemble (e.g., buffer) a group of messages and send thegroup of messages to the Q node. The streamlet can allocate a block ofmemory space from the Q node and store the group of messages in theblock. The MX node can also perform compression on the group ofmessages. For example, the MX node can remove a common header from eachmessage,

FIG. 5 is a flowchart of an example method 500 for storing messages in amessaging system. The method can be implemented, for example, using a Qnode such as the Q node 208 in the messaging system 100. The method 500begins by receiving from a plurality of publishers a plurality ofmessages, each of the messages being assigned to one of a plurality ofdistinct channels wherein each channel comprises an ordered plurality ofmessages (502). The message stores each message of each of the channelsin a respective buffer, each buffer having a time-to-live (504). Duringthe storing, the method 500 removes one or more of the buffers havingrespective time-to-lives that have expired (506). The method providesmessages for one or more of the channels to one or more subscribers froma plurality of the remaining buffers according to the order (508).

Embodiments of the subject matter and the operations described in thisspecification can be implemented in digital electronic circuitry, or incomputer software, firmware, or hardware, including the structuresdisclosed in this specification and their structural equivalents, or incombinations of one or more of them. Embodiments of the subject matterdescribed in this specification can be implemented as one or morecomputer programs, i.e., one or more modules of computer programinstructions, encoded on computer storage medium for execution by, or tocontrol the operation of, data processing apparatus. Alternatively or inaddition, the program instructions can be encoded on anartificially-generated propagated signal, e.g., a machine-generatedelectrical, optical, or electromagnetic signal, that is generated toencode information for transmission to suitable receiver apparatus forexecution by a data processing apparatus. A computer storage medium canbe, or be included in, a computer-readable storage device, acomputer-readable storage substrate, a random or serial access memoryarray or device, or a combination of one or more of them. Moreover,while a computer storage medium is not a propagated signal, a computerstorage medium can be a source or destination of computer programinstructions encoded in an artificially-generated propagated signal. Thecomputer storage medium can also be, or be included in, one or moreseparate physical components or media (e.g., multiple CDs, disks, orother storage devices).

The operations described in this specification can be implemented asoperations performed by a data processing apparatus on data stored onone or more computer-readable storage devices or received from othersources.

The term “data processing apparatus” encompasses all kinds of apparatus,devices, and machines for processing data, including by way of example aprogrammable processor, a computer, a system on a chip, or multipleones, or combinations, of the foregoing The apparatus can includespecial purpose logic circuitry, e.g., an FPGA (field programmable gatearray) or an ASIC (application-specific integrated circuit). Theapparatus can also include, in addition to hardware, code that createsan execution environment for the computer program in question, e.g.,code that constitutes processor firmware, a protocol stack, a databasemanagement system, an operating system, a cross-platform runtimeenvironment, a virtual machine, or a combination of one or more of them.The apparatus and execution environment can realize various differentcomputing model infrastructures, such as web services, distributedcomputing and grid computing infrastructures.

A computer program (also known as a program, software, softwareapplication, script, or code) can be written in any form of programminglanguage, including compiled or interpreted languages, declarative,procedural, or functional languages, and it can be deployed in any form,including as a stand-alone program or as a module, component,subroutine, object, or other unit suitable for use in a computingenvironment. A computer program may, but need not, correspond to a filein a file system. A program can be stored in a portion of a file thatholds other programs or data (e.g., one or more scripts stored in amarkup language resource), in a single file dedicated to the program inquestion, or in multiple coordinated files (e.g., files that store oneor more modules, sub-programs, or portions of code). A computer programcan be deployed to be executed on one computer or on multiple computersthat are located at one site or distributed across multiple sites andinterconnected by a communication network.

The processes and logic flows described in this specification can beperformed by one or more programmable processors executing one or morecomputer programs to perform actions by operating on input data andgenerating output. The processes and logic flows can also be performedby, and apparatus can also be implemented as, special purpose logiccircuitry, e.g., an FPGA (field programmable gate array) or an ASIC(application-specific integrated circuit).

Processors suitable for the execution of a computer program include, byway of example, both general and special purpose microprocessors, andany one or more processors of any kind of digital computer. Generally, aprocessor will receive instructions and data from a read-only memory ora random access memory or both. The essential elements of a computer area processor for performing actions in accordance with instructions andone or more memory devices for storing instructions and data. Generally,a computer will also include, or be operatively coupled to receive datafrom or transfer data to, or both, one or more mass storage devices forstoring data, e.g., magnetic, magneto-optical disks, or optical disks.However, a computer need not have such devices. Moreover, a computer canbe embedded in another device, e.g., a smart phone, a mobile audio orvideo player, a game console, a Global Positioning System (GPS)receiver, or a portable storage device (e.g., a universal serial bus(USB) flash drive), to name just a few. Devices suitable for storingcomputer program instructions and data include all forms of non-volatilememory, media and memory devices, including by way of examplesemiconductor memory devices, e.g., EPROM, EEPROM, and flash memorydevices; magnetic disks, e.g., internal hard disks or removable disks;magneto-optical disks; and CD-ROM and DVD-ROM disks. The processor andthe memory can be supplemented by, or incorporated in, special purposelogic circuitry.

To provide for interaction with a user, embodiments of the subjectmatter described in this specification can be implemented on a computerhaving a display device, e.g., a CRT (cathode ray tube) or LCD (liquidcrystal display) monitor, for displaying information to the user and akeyboard and a pointing device, e.g., a mouse or a trackball, by whichthe user can provide input to the computer. Other kinds of devices canbe used to provide for interaction with a user as well; for example,feedback provided to the user can be any form of sensory feedback, e.g.,visual feedback, auditory feedback, or tactile feedback; and input fromthe user can be received in any form, including acoustic, speech, ortactile input. In addition, a computer can interact with a user bysending resources to and receiving resources from a device that is usedby the user; for example, by sending web pages to a web browser on auser's client device in response to requests received from the webbrowser.

Embodiments of the subject matter described in this specification can beimplemented in a computing system that includes a back-end component, asa data server, or that includes a middleware component, e.g., anapplication server, or that includes a front-end component, e.g., aclient computer having a graphical user interface or a Web browserthrough which a user can interact with an implementation of the subjectmatter described in this specification, or any combination of one ormore such back-end, middleware, or front-end components. The componentsof the system can be interconnected by any form or medium of digitaldata communication, e.g., a communication network. Examples ofcommunication networks include a local area network (“LAN”) and a widearea network (“WAN”), an inter-network (e.g., the Internet), andpeer-to-peer networks (e.g., ad hoc peer-to-peer networks).

The computing system can include clients and servers. A client andserver are generally remote from each other and typically interactthrough a communication network. The relationship of client and serverarises by virtue of computer programs running on the respectivecomputers and having a client-server relationship to each other. In someembodiments, a server transmits data (e.g., an HTML page) to a clientdevice (e.g., for purposes of displaying data to and receiving userinput from a user interacting with the client device). Data generated atthe client device (e.g., a result of the user interaction) can hereceived from the client device at the server.

A system of one or more computers can be configured to performparticular operations or actions by virtue of having software, firmware,hardware, or a combination of them installed on the system that inoperation causes or cause the system to perform the actions. One or morecomputer programs can be configured to perform particular operations oractions by virtue of including instructions that, when executed by dataprocessing apparatus, cause the apparatus to perform the actions.

While this specification contains many specific implementation details,these should not be construed as limitations on the scope of anyinventions or of what may he claimed, but rather as descriptions offeatures specific to particular embodiments of particular inventions.Certain features that are described in this specification in the contextof separate embodiments can also be implemented in combination in asingle embodiment. Conversely, various features that are described inthe context of a single embodiment can also be implemented in multipleembodiments separately or in any suitable subcombination. Moreover,although features may be described above as acting in certaincombinations and even initially claimed as such, one or more featuresfrom a claimed combination can in some cases be excised from thecombination, and the claimed combination may be directed to asubcombination or variation of a subcombination.

Similarly, while operations are depicted in the drawings in a particularorder, this should not be understood as requiring that such operationsbe performed in the particular order shown or in sequential order, orthat all illustrated operations be performed, to achieve desirableresults. In certain circumstances, multitasking and parallel processingmay be advantageous. Moreover, the separation of various systemcomponents in the embodiments described above should not be understoodas requiring such separation in all embodiments, and it should beunderstood that the described program components and systems cangenerally be integrated together in a single software product orpackaged into multiple software products.

Thus, particular embodiments of the subject matter have been described.Other embodiments are within the scope of the following claims. In somecases, the actions recited in the claims can be performed in a differentorder and still achieve desirable results. In addition, the processesdepicted in the accompanying figures do not necessarily require theparticular order shown, or sequential order, to achieve desirableresults. In certain implementations, multitasking and parallelprocessing may be advantageous.

What is claimed is:
 1. A method, comprising: storing, by One or morecomputer processors, messages of each of a plurality of channels in oneor more respective buffers, wherein each buffer comprises a respectivebuffer time-to-live, wherein each buffer comprises a plurality ofblocks, wherein each block of the plurality of blocks stores one or moreof the messages, wherein each block comprises a respective blocktime-to-live, and wherein the block time-to-live is different than thebuffer time-to-live; and sending, by the one or more computerprocessors, the messages for a channel of the plurality of channelsretrieved from one or more blocks that have not expired.
 2. The methodof claim 1, wherein storing the messages of each of the plurality ofchannels in the one or more respective buffers comprises: storing themessages received at an earlier time in blocks having respective blocktime-to-lives that will expire sooner than blocks used to store messagesreceived at a later time.
 3. The method of claim 1, wherein the blocktime-to-live is shorter than the buffer time-to-live.
 4. The method ofclaim 1, comprising removing one or more of the blocks having respectiveblock time-to-lives that have expired.
 5. The method of claim 4, whereinthe retrieved messages persist in unremoved blocks until respectiveblock time-to-lives of the unremoved blocks expire.
 6. The method ofclaim 1, wherein each channel comprises an ordered plurality ofmessages.
 7. The method of claim 1, comprising: arranging the storedmessages in the buffer according to when the messages were received. 8.The method of claim 1, comprising: removing the buffers when the buffertime-to-live tear the buffer has expired.
 9. The method of claim 1,comprising sending the messages for the channel to one or moresubscriber clients subscribed to the channel after a predetermined timeperiod.
 10. The method of claim 1, wherein the messages are receivedfrom one or more publishers, and wherein each of the messages isassociated with a respective channel of the plurality of channels.
 11. Asystem, comprising: one or more computer processors programmed toperform operations to: store messages of each of a plurality of channelsin one or more respective buffers, wherein each buffer comprises arespective buffer time-to-live, wherein each buffer comprises aplurality of blocks, wherein each block of the plurality of blocksstores one or more of the messages, wherein each block comprises arespective block time-to-live, and wherein the block time-to-live isdifferent than the buffer time-to-live; and send the messages for achannel of the plurality of channels retrieved from one or more blocksthat have not expired.
 12. The system of claim 11, wherein to store themessages of each of the plurality of channels in the one or morerespective buffers the one or more computer processors are further to:store the messages received at an earlier time in blocks havingrespective block time-to-lives that will expire sooner than blocks usedto store messages received at a later time.
 13. The system of claim 11,wherein the block time-to-live is shorter than the buffer time-to-live.14. The system of claim 11, wherein the operations are further to:remove one or more of the blocks having respective block time-to-livesthat have expired.
 15. The system of claim 14, wherein the retrievedmessages persist in unremoved blocks until respective blocktime-to-lives of the unremoved blocks expire.
 16. The system of claim11, wherein each channel comprises an ordered plurality of messages. 17.The system of claim 11, wherein the operations are further to: arrangethe stored messages in the buffer according to when the messages werereceived.
 18. The system of claim 11, wherein the operations are furtherto: remove the buffers when the buffer time-to-live for the buffer hasexpired.
 19. The system of claim 11, wherein the one or more computerprocessors are further to send the messages for the channel to one ormore subscriber clients subscribed to the channel after a predeterminedtime period.
 20. A non-transitory computer-readable medium havinginstructions stored thereon that, when executed by one or more computerprocessors, cause the one or more computer processors to: store messagesof each of a plurality of channels in one or more respective buffers,wherein each buffer comprises a respective buffer time-to-live, whereineach buffer comprises a plurality of blocks, wherein each block of theplurality of blocks stores one or more of the messages, wherein eachblock comprises a respective block time-to-live, and wherein the blocktime-to-live is different than the buffer time-to-live; and send themessages for a channel of the plurality of channels retrieved from oneor more blocks that have not expired.